package org.jeecg.modules.oms.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.jeecg.common.api.vo.Result;
import org.jeecg.common.system.query.QueryGenerator;
import org.jeecg.modules.oms.entity.ProductSalesOrderDetail;
import org.jeecg.modules.oms.service.ICfWindowDataService;
import org.jeecg.modules.oms.service.IProductSalesOrderDetailService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.jeecg.common.aspect.annotation.AutoLog;

/**
 * 成品销售订单明细表
 */
@Api(tags="成品销售订单明细")
@RestController
@RequestMapping("/oms/productSalesOrder/detail")
@Slf4j
public class ProductSalesOrderDetailController extends JeecgController<ProductSalesOrderDetail, IProductSalesOrderDetailService> {
    @Autowired
    private IProductSalesOrderDetailService productSalesOrderDetailService;
    
    @Autowired
    private ICfWindowDataService cfWindowDataService;
    
    /**
     * 分页列表查询
     */
    @AutoLog(value = "成品销售订单明细表-分页列表查询")
    @ApiOperation(value="成品销售订单明细表-分页列表查询", notes="成品销售订单明细表-分页列表查询")
    @GetMapping(value = "/pageList")
    public Result<?> queryPageList(ProductSalesOrderDetail productSalesOrderDetail,
                                @RequestParam(name="pageNo", defaultValue="1") Integer pageNo,
                                @RequestParam(name="pageSize", defaultValue="10") Integer pageSize,
                                HttpServletRequest req) {
        QueryWrapper<ProductSalesOrderDetail> queryWrapper = QueryGenerator.initQueryWrapper(productSalesOrderDetail, req.getParameterMap());
        Page<ProductSalesOrderDetail> page = new Page<ProductSalesOrderDetail>(pageNo, pageSize);
        IPage<ProductSalesOrderDetail> pageList = productSalesOrderDetailService.page(page, queryWrapper);
        return Result.OK(pageList);
    }
    
    /**
     * 添加
     */
    @AutoLog(value = "成品销售订单明细表-添加")
    @ApiOperation(value="成品销售订单明细表-添加", notes="成品销售订单明细表-添加")
    @PostMapping(value = "/add")
    public Result<?> add(@RequestBody ProductSalesOrderDetail productSalesOrderDetail) {
        productSalesOrderDetailService.save(productSalesOrderDetail);
        return Result.OK("添加成功！");
    }
    
    /**
     * 编辑
     */
    @AutoLog(value = "成品销售订单明细表-编辑")
    @ApiOperation(value="成品销售订单明细表-编辑", notes="成品销售订单明细表-编辑")
    @PutMapping(value = "/edit")
    public Result<?> edit(@RequestBody ProductSalesOrderDetail productSalesOrderDetail) {
        productSalesOrderDetailService.updateById(productSalesOrderDetail);
        return Result.OK("编辑成功!");
    }
    
    /**
     * 通过id删除
     */
    @AutoLog(value = "成品销售订单明细表-通过id删除")
    @ApiOperation(value="成品销售订单明细表-通过id删除", notes="成品销售订单明细表-通过id删除")
    @DeleteMapping(value = "/delete")
    public Result<?> delete(@RequestParam(name="id",required=true) String id) {
        // 删除订单明细相关的所有子表数据
        cfWindowDataService.deleteAllByDetailId(id);
        // 删除订单明细
        productSalesOrderDetailService.removeById(id);
        return Result.OK("删除成功!");
    }
    
    /**
     * 批量删除
     */
    @AutoLog(value = "成品销售订单明细表-批量删除")
    @ApiOperation(value="成品销售订单明细表-批量删除", notes="成品销售订单明细表-批量删除")
    @DeleteMapping(value = "/deleteBatch")
    public Result<?> deleteBatch(@RequestParam(name="ids",required=true) String ids) {
        List<String> idList = Arrays.asList(ids.split(","));
        // 删除订单明细相关的所有子表数据
        for (String id : idList) {
            cfWindowDataService.deleteAllByDetailId(id);
        }
        // 删除订单明细
        productSalesOrderDetailService.removeByIds(idList);
        return Result.OK("批量删除成功!");
    }
    
    /**
     * 通过id查询
     */
    @AutoLog(value = "成品销售订单明细表-通过id查询")
    @ApiOperation(value="成品销售订单明细表-通过id查询", notes="成品销售订单明细表-通过id查询")
    @GetMapping(value = "/queryById")
    public Result<?> queryById(@RequestParam(name="id",required=true) String id) {
        ProductSalesOrderDetail productSalesOrderDetail = productSalesOrderDetailService.getById(id);
        if(productSalesOrderDetail==null) {
            return Result.error("未找到对应数据");
        }
        return Result.OK(productSalesOrderDetail);
    }
    
    /**
     * 根据订单ID获取订单明细列表
     */
    @AutoLog(value = "成品销售订单明细表-根据订单ID获取明细列表")
    @ApiOperation(value="成品销售订单明细表-根据订单ID获取明细列表", notes="用于成品入库时获取订单明细信息")
    @GetMapping(value = "/getDetailsByOrderId")
    public Result<?> getDetailsByOrderId(@RequestParam(name="orderId", required=true) String orderId) {
        try {
            QueryWrapper<ProductSalesOrderDetail> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("order_id", orderId);
            queryWrapper.orderByAsc("window_no");
            
            List<ProductSalesOrderDetail> detailList = productSalesOrderDetailService.list(queryWrapper);
            return Result.OK(detailList);
        } catch (Exception e) {
            log.error("获取订单明细失败", e);
            return Result.error("获取订单明细失败: " + e.getMessage());
        }
    }
    
    /**
     * 批量删除临时保存的明细数据
     * 前端上传文件解析后，如果用户没有点击确认提交，
     * 需要删除已经保存的相关表数据
     */
    @AutoLog(value = "成品销售订单明细表-批量删除临时数据")
    @ApiOperation(value="成品销售订单明细表-批量删除临时数据", notes="成品销售订单明细表-批量删除临时数据")
    @PostMapping(value = "/batchDelete")
    public Result<?> batchDelete(@RequestBody Map<String, List<String>> params) {
        List<String> detailIds = params.get("detailIds");
        if (detailIds == null || detailIds.isEmpty()) {
            return Result.error("未提供需要删除的detailIds");
        }
        
        try {
            // 逐个删除明细及其关联数据
            for (String detailId : detailIds) {
                // 1. 删除相关子表数据
                cfWindowDataService.deleteAllByDetailId(detailId);
                
                // 2. 删除明细主表数据
                productSalesOrderDetailService.removeById(detailId);
            }
            
            return Result.OK("批量删除临时数据成功");
        } catch (Exception e) {
            log.error("批量删除临时数据失败", e);
            return Result.error("批量删除临时数据失败: " + e.getMessage());
        }
    }
} 